//
//  SEGAuthorCharacterViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/23.
//

#import "SEGAuthorCharacterViewModel.h"
@interface SEGAuthorCharacterViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;
@end

@implementation SEGAuthorCharacterViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
}

- (void)racInit {
    
    //@weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self isShowSVP:YES];
            
            
            SEGQueryauthinfoRoleauthmapModel *roleAuthMap = [SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo;
            [[SEGSelectauthorityroleManager sharedManager] requestSelectauthorityroleWithCommunityid:kAccountCommunityID
                                                                                        enterpriseId:roleAuthMap.enterpriseId
                                                                                             organId:[SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo.organId
                                                                                              propId:roleAuthMap.propId
                                                                                            finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowSVP:NO];
                
                if (!isSuccessed || [SEGSelectauthorityroleManager sharedManager].SelectAuthorityRole.data.list.count <= 0) {
                    [SEGAlertView showTmepMessageWithMessage:errorMessage];
                    [self showMessage:errorMessage];
                    [subscriber sendNext:@{@"code":@"0",@"msg":@""}];
                    [subscriber sendCompleted];
                    return;
                }
                
                __block NSMutableArray *tmp = [NSMutableArray array];
                [[SEGSelectauthorityroleManager sharedManager].SelectAuthorityRole.data.list enumerateObjectsUsingBlock:^(SEGSelectauthorityroleListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (![obj.isAdmin isEqualToString:@"1"]) [tmp addObject:obj];
                }];
                
                [subscriber sendNext:@{@"code":@"1",@"msg":@"",@"data":tmp}];
                [subscriber sendCompleted];
                return;
            }];
            
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowSVP:(BOOL)show {
    [self.messageSubject sendNext:@{@"code": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"3",@"msg":msg}];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter

@end
